package com.hbnu.exercise.net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo05
{
    public static void main(String[] args)
    {
        try
        {
            ServerSocket serverSocket = new ServerSocket(8888);

            Socket socket = serverSocket.accept();

            InputStream in = socket.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\test.jpg"));

            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);

            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = in.read(bytes)) != -1)
            {
                fileOutputStream.write(bytes, 0, length);
            }

            out.println("图片上传成功");

            fileOutputStream.close();
            serverSocket.close();
        } catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}
